package com.xiaomazi.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xiaomazi.common.utils.R;
import com.xiaomazi.edu.client.VideoClient;
import com.xiaomazi.edu.entity.Video;
import com.xiaomazi.edu.service.ChapterService;
import com.xiaomazi.edu.service.VideoService;
import com.xiaomazi.edu.vo.ChapterResult;
import com.xiaomazi.edu.vo.VideoData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaomazi.edu.mapper.ChapterMapper;
import com.xiaomazi.edu.entity.Chapter;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {


    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoClient videoClient;

    /**
     * 根据课程Id查询出章节列表数据
     * @param courseId 课程Id
     * @return ChapterResult集合
     */
    @Override
    public List<ChapterResult> getChapterInfoByCourseId(String courseId) {
        List<Chapter> chapterList = this.query().eq("course_id", courseId).list();

        List<Video> videoList = videoService.query().eq("course_id", courseId).list();

        List<ChapterResult> chapterResultList = new ArrayList<>();

        for (Chapter chapter : chapterList) {
            ChapterResult chapterResult = new ChapterResult();
            BeanUtil.copyProperties(chapter,chapterResult);
            List<VideoData> videoDataList = chapterResult.getVideoDataList();
            for (Video video : videoList) {
                if (video.getChapterId().equals(chapter.getId())){
                    VideoData videoData = new VideoData();
                    BeanUtil.copyProperties(video,videoData);
                    videoDataList.add(videoData);
                }
            }
            chapterResultList.add(chapterResult);
        }

        return chapterResultList;
    }

    /**
     * 删除章节
     * @param chapterId 章节Id
     * @return ok error
     */
    @Override
    @Transactional
    public R removeChapterAndVideo(String chapterId) {
        List<Video> videoList = videoService.query().eq("chapter_id", chapterId).list();
        if (null == videoList || videoList.isEmpty()){
            return this.removeById(chapterId) ? R.ok() : R.error();
        }
        List<String> videoIds = videoList.stream().map(Video::getId).collect(Collectors.toList());
        List<String> videoSourceIds = videoList.stream().map(Video::getVideoSourceId).collect(Collectors.toList());
        return videoClient.deleteBatch(videoSourceIds).getSuccess()
                && videoService.removeBatchByIds(videoIds)
                && this.removeById(chapterId)
                ? R.ok() : R.error();
    }

    /**
     * 根据课程id删除所有的章节
     * @param courseId 课程id
     */
    @Override
    public void deleteChapterByCourseId(String courseId) {
        this.update().eq("course_id",courseId).remove();
    }
}
